﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.MODEL.PrintManage;
using MINERP.MODEL.FoundationModel;
using MINERP.BLL.PrintManage;
using DevExpress.XtraEditors;
using MINERP.BLL.Common;
using MINERP.COM;
using FastReport;

namespace MINERP.Frontier
{
    public partial class FrmAddPaperWasteRecord : Form
    {
        private PaperInfoModel _paperInfo = null;

        public PaperInfoModel PaperInfo
        {
            set { _paperInfo = value; }
        }

        public FrmAddPaperWasteRecord()
        {
            InitializeComponent();
        }

        private void FrmAddPaperWasteRecord_Load(object sender, EventArgs e)
        {
            LinkPaperWasteType();
            txtPaperId.Text = _paperInfo.PrintPaperId.ToString();
        }

        /// <summary>
        /// 绑定纸浪费类型
        /// </summary>
        private void LinkPaperWasteType()
        {
            PaperWasteTypeBLL pWasteType = new PaperWasteTypeBLL();
            cmbPaperWasteType.Properties.Items.Clear();
            foreach (var item in pWasteType.GetPaperWasteTypeInfoList())
            {
                cmbPaperWasteType.Properties.Items.Add(item.PaperOperationTypeName);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            PaperWasteTypeBLL pWasteType = new PaperWasteTypeBLL();
            PaperWasteRecord pWasteRecord = new PaperWasteRecord();
            int paperWasteRecordId = 0;
            if (DataCheck())
            {
                PaperInfoModel piTemp = new PaperInfoModel();
                int paperId = Convert.ToInt32(txtPaperId.Text.Trim());
                if (paperId == _paperInfo.PrintPaperId)
                {
                    piTemp = _paperInfo;
                }
                else
                {
                    PaperBLL pb = new PaperBLL();
                    piTemp = pb.GetPaper(paperId);
                }
                if (piTemp == null)
                {
                    MessageBox.Show(string.Format("没有找到 {0}号纸,请重新输入", paperId), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                PaperWasteType_info pwastetypeinfo = pWasteType.GetPaperWasteTypeInfo(cmbPaperWasteType.Text);
                if (pwastetypeinfo == null)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                PaperWasteRecordModel pWasteRecordInfo = new PaperWasteRecordModel();
                pWasteRecordInfo.CreateTime = UnityMethod.GetSystemTime();
                pWasteRecordInfo.Length = Convert.ToDouble(txtPaperWasteLength.Text) / 100;
                pWasteRecordInfo.PaperInfo = piTemp;
                pWasteRecordInfo.PaperWasteType = pwastetypeinfo;
                pWasteRecordInfo.Remark = string.Empty;
                pWasteRecordInfo.Status = (int)WasterPaperStatus.unSure;
                pWasteRecordInfo.Width = _paperInfo.Width;
                int ret = pWasteRecord.Insert(pWasteRecordInfo, out paperWasteRecordId);
                if (ret == -1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 0)
                {
                    if (!chkPrintItSelf.Checked)//如果非本软件出的片则需要减去长度
                    {
                        PaperBLL paperBLL = new PaperBLL();
                        //改变数据库里纸的长度
                        PaperInfoModel _paper = paperBLL.GetPaper(paperId);
                        _paper.LastLength -= Convert.ToDouble(txtPaperWasteLength.Text) / 100;
                        _paper.PaperConsumptionLength += Convert.ToDouble(txtPaperWasteLength.Text) / 100;
                        _paper.InvalidationArea += _paper.Width * (Convert.ToDouble(txtPaperWasteLength.Text) / 100);//浪费面积
                        paperBLL.Update(_paper);
                    }
                    FrmPrintBarcode frmBarcode = new FrmPrintBarcode();
                    frmBarcode.BarCode = paperWasteRecordId;
                    frmBarcode.ShowDialog();
                }
                this.DialogResult = DialogResult.OK;
            }
        }
        private bool DataCheck()
        {
            if (IsTextNull(txtPaperWasteLength, "浪费长度"))
            {
                return false;
            }
            if (string.IsNullOrEmpty(cmbPaperWasteType.Text.Trim()) || cmbPaperWasteType.Text == "请选择废纸类型")
            {
                MessageBox.Show("请选择纸浪费类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;

            }

            try
            {
                if (Convert.ToInt32(txtPaperId.Text.Trim()) < 1)
                {
                    MessageBox.Show("请输入正确的纸ID", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确的纸ID", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return true;
        }

        private bool IsTextNull(TextEdit text, string name)
        {
            if (string.IsNullOrEmpty(text.Text.Trim()))
            {
                MessageBox.Show("请输入" + name, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                text.Focus();
                return true;
            }
            return false;
        }
    }
}
